package com.CNMedical.system.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.diboot.core.service.impl.BaseServiceImpl;
import com.CNMedical.common.core.domain.R;
import com.CNMedical.common.utils.DateUtils;
import com.CNMedical.system.domain.vo.AppletLearningHistoryVO;
import org.springframework.stereotype.Service;
import com.CNMedical.system.mapper.AppletLearningHistoryMapper;
import com.CNMedical.system.domain.entity.AppletLearningHistory;
import com.CNMedical.system.service.IAppletLearningHistoryService;

import javax.annotation.Resource;

/**
 * 学习历史Service业务层处理
 * 
 * @author tang
 * @date 2023-03-28
 */
@Service
public class AppletLearningHistoryServiceImpl extends BaseServiceImpl<AppletLearningHistoryMapper, AppletLearningHistory> implements IAppletLearningHistoryService
{
    @Resource
    private AppletLearningHistoryMapper appletLearningHistoryMapper;

    @Override
    public R addLearningHistory(AppletLearningHistory appletLearningHistory) {

        if (!lambdaQuery().eq(AppletLearningHistory::getKnowledgeId,appletLearningHistory.getKnowledgeId())
                .eq(AppletLearningHistory::getUserId,appletLearningHistory.getUserId()).exists()) {
            this.save(appletLearningHistory);
        }
        return R.ok();
    }

    @Override
    public List<AppletLearningHistoryVO> getLearningHistoryByUserId(String userId) {
        List<AppletLearningHistoryVO> viewObjectList = getViewObjectList(Wrappers.lambdaQuery(AppletLearningHistory.class).eq(AppletLearningHistory::getUserId, userId),
                null,
                AppletLearningHistoryVO.class);
        return viewObjectList;
    }

    /**
     * 查询学习历史
     * 
     * @param id 学习历史主键
     * @return 学习历史
     */
    @Override
    public AppletLearningHistory selectAppletLearningHistoryById(Long id)
    {
        return appletLearningHistoryMapper.selectAppletLearningHistoryById(id);
    }

    /**
     * 查询学习历史列表
     * 
     * @param appletLearningHistory 学习历史
     * @return 学习历史
     */
    @Override
    public List<AppletLearningHistory> selectAppletLearningHistoryList(AppletLearningHistory appletLearningHistory)
    {
        return appletLearningHistoryMapper.selectAppletLearningHistoryList(appletLearningHistory);
    }

    /**
     * 新增学习历史
     * 
     * @param appletLearningHistory 学习历史
     * @return 结果
     */
    @Override
    public int insertAppletLearningHistory(AppletLearningHistory appletLearningHistory)
    {
        appletLearningHistory.setCreateTime(DateUtils.getNowDate());
        return appletLearningHistoryMapper.insertAppletLearningHistory(appletLearningHistory);
    }

    /**
     * 修改学习历史
     * 
     * @param appletLearningHistory 学习历史
     * @return 结果
     */
    @Override
    public int updateAppletLearningHistory(AppletLearningHistory appletLearningHistory)
    {
        appletLearningHistory.setUpdateTime(DateUtils.getNowDate());
        return appletLearningHistoryMapper.updateAppletLearningHistory(appletLearningHistory);
    }

    /**
     * 批量删除学习历史
     * 
     * @param ids 需要删除的学习历史主键
     * @return 结果
     */
    @Override
    public int deleteAppletLearningHistoryByIds(Long[] ids)
    {
        return appletLearningHistoryMapper.deleteAppletLearningHistoryByIds(ids);
    }

    /**
     * 删除学习历史信息
     * 
     * @param id 学习历史主键
     * @return 结果
     */
    @Override
    public int deleteAppletLearningHistoryById(Long id)
    {
        return appletLearningHistoryMapper.deleteAppletLearningHistoryById(id);
    }
}
